package com.sumbo.config.writer;

import com.sumbo.config.reader.ReaderConfig;

import java.io.IOException;
import java.io.InputStream;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * \* User: MeiZhongHao
 * \* Date: 2019-06-10
 * \* Time: 10:32
 * \* Description: 输出配置类
 * \
 */
public class WriterConfig {

    private static Lock kafkalock = new ReentrantLock();

    private static Lock rocketMqlock = new ReentrantLock();

    private static Map<String, Properties> rocketMqConfig = new ConcurrentHashMap<>();

    private static Map<String, Properties> kafkaConfig = new ConcurrentHashMap<>();

    static {
        Properties prop = new Properties();
        InputStream in = ReaderConfig.class.getClassLoader().getResourceAsStream(
                "config.properties");
        try {
            prop.load(in);
            initKafkaConfig(prop);
            initRocketMqConfig(prop);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static void initKafkaConfig(Properties prop) {
        String names;
        Map<String, Properties> temp = new ConcurrentHashMap<>();
        if (prop.containsKey("writer.kafka.name") && (names = prop.getProperty("writer.kafka.name")) != null && names.length() > 0) {
            String[] readers = names.split(",");
            for (String reader : readers) {
                String pre = "writer.kafka." + reader + ".";
                Properties properties = new Properties();
                for (String key : prop.stringPropertyNames()) {
                    if (key.startsWith(pre)) {
                        properties.setProperty(key.replace(pre, ""), prop.getProperty(key));
                    }
                }
                temp.put(reader, properties);
            }
        }
        try {
            kafkalock.lock();
            kafkaConfig = temp;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            kafkalock.unlock();
        }
    }

    private static void initRocketMqConfig(Properties prop) {
        String names;
        Map<String, Properties> temp = new ConcurrentHashMap<>();
        if (prop.containsKey("writer.rocketMq.name") && (names = prop.getProperty("writer.rocketMq.name")) != null && names.length() > 0) {
            String[] readers = names.split(",");
            for (String reader : readers) {
                String pre = "writer.rocketMq." + reader + ".";
                Properties properties = new Properties();
                for (String key : prop.stringPropertyNames()) {
                    if (key.startsWith(pre)) {
                        properties.setProperty(key.replace(pre, ""), prop.getProperty(key));
                    }
                }
                temp.put(reader, properties);
            }
        }
        try {
            rocketMqlock.lock();
            rocketMqConfig = temp;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            rocketMqlock.unlock();
        }
    }

    public static Map<String, Properties> getKafkaConfig() {
        return kafkaConfig;
    }

    public static Map<String, Properties> getRocketMqConfig() {
        return rocketMqConfig;
    }
}